// 页面路径：store/index.js 
import Vue from 'vue'
import Vuex from 'vuex'
import { getLogin } from '../api/user.js'

Vue.use(Vuex); //vue的插件机制

//Vuex.Store 构造器选项
const store = new Vuex.Store({
	state: { //存放状态
		authInfo: {},
		goods:{}
	},
	mutations:{
		 userInfo(state, data) {
		    state.authInfo = data
		},
		setGoods(state,data){
			state.goods = data
		}
	},
	actions: {
		// 登录
		getLogins({commit,state},code){
			let that = this;
			return new Promise((resolve, reject)=>{
				// console.log('code',code);
				let data = getLogin(code).then(res=>{
					// console.log(res[1].data.data.access_token);
					if(res.data.data.user.phone != ''||res.data.data.user.phone != undefined || res.data.data.user.phone != null){
						uni.setStorageSync('access_token', res.data.data.access_token);
						// that.authInfo = ;
						store.commit('userInfo',res.data.data.user)
						uni.setStorageSync('userInfo',res.data.data.user)
						uni.setStorageSync('question_error',res.data.data.user.question_count - res.data.data.user.correct_rate * res.data.data.user.question_count)
						uni.setStorageSync('question_count',res.data.data.user.question_count)
						// console.log(res[1].data.data.user);
						resolve(res)
					}
				});
				// console.log(data);
			})
		},
		saveGoods({commit,state},data){
			console.log("保存",data);
			let that = this
			store.commit('setGoods',data)
		}
	},
})
export default store